<!DOCTYPE html>
<html lang="en">
<head>
    <title>公众号文章</title>
    {% include 'admin/common/header.html' %}
    <link rel="stylesheet" href="{{ url_for('static', filename='admin/admin/css/other/user.css') }}"/>
</head>
<body class="pear-container">
{# 查询表单 #}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="article-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">公众号名称</label>
                <div class="layui-input-inline">
<!--                    <input type="hidden" id="wx_id" name="wx_id">-->
                    <input type="text" name="wx_name" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">文章名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="article_title" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="article-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

{# 用户表格 #}
<div class="user-main user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="article-table" lay-filter="article-table"></table>
        </div>
    </div>
</div>
</body>
{# 表格操作 #}
<script type="text/html" id="user-toolbar">
    {% if authorize("admin:article:add") %}
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="pear-icon pear-icon-add"></i>
            新增
        </button>
    {% endif %}
    {% if authorize("admin:article:remove") %}
        <button class="pear-btn pear-btn-md" lay-event="batchRemove">
            <i class="pear-icon pear-icon-ashbin"></i>
            删除
        </button>
    {% endif %}

</script>

{# 用户修改操作 #}
<script type="text/html" id="user-bar">
<!--    {% if authorize("admin:article:remove") %}-->
<!--        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="pear-icon pear-icon-edit"></i>-->
<!--        </button>-->
<!--    {% endif %}-->
    {% if authorize("admin:article:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="pear-icon pear-icon-ashbin"></i>
        </button>
    {% endif %}
</script>

{# 文章链接 #}
<script type="text/html" id="titleUrl">
  <a href='{{ "{{ d.article_url }}" }}' target="_blank" class="layui-table-link">{{ "{{ d.article_title }}" }}</a>
</script>

{# 用户注册时间 #}
<script type="text/html" id="article-createTime">
    {{ '  {{layui.util.toDateString(d.create_at,  "yyyy-MM-dd HH:mm:ss")}' |safe }}}
</script>


{% include 'admin/common/footer.html' %}

<script>

  layui.use(['table','layer', 'dtree', 'form', 'jquery', 'popup', 'common'], function () {
    let table = layui.table
    let form = layui.form
    let $ = layui.jquery
    let dtree = layui.dtree
    let popup = layui.popup
    let common = layui.common
    let MODULE_PATH = '/admin/wxOfficialAcc/'
    let router = layui.router()
    //let wxid = request.wxid

    //$('#wx_id').val(wxid);




    // 表格数据
    let cols = [
      [
        {% if authorize("admin:article:remove") %}
          { type: 'checkbox' },
        {% endif %}
        { title: '公众号', field: 'wx_name', align: 'center', width: 110 ,sort: true},
        { title: '标题', field: 'article_title', align: 'center', templet: '#titleUrl'},
        { title: '链接', field: 'article_url', align: 'center',width: 410 },
        { title: '采集机器人', field: 'robot_name', align: 'center', width: 180 ,sort: true},
        //{ title: '采集时间', field: 'create_at', templet: '#article-createTime', align: 'center' },
        { title: '采集时间', field: 'create_at',templet: function(d){
        var timestr = layui.util.toDateString(new Date(d.create_at).getTime()-(8*3600000), "yyyy-MM-dd HH:mm:ss");
        return '<div>'+ timestr +'</div>'}, align: 'center' ,sort: true},
        { title: '操作', toolbar: '#user-bar', align: 'center', width: 100 }
      ]
    ]

    // 渲染表格数据
    table.render({
      elem: '#article-table',
      url: MODULE_PATH + 'articleData',
      page: true,
      cols: cols,
      skin: 'line',
      height: 'full-148',
      toolbar: '#user-toolbar', /*工具栏*/
      text: { none: '暂无人员信息' },
      defaultToolbar: [{ layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'print', 'exports'] /*默认工具栏*/
    })




    table.on('tool(article-table)', function (obj) {
      if (obj.event === 'remove') {
        window.remove(obj)
      } else if (obj.event === 'edit') {
        window.edit(obj)
      }
    })

    table.on('toolbar(article-table)', function (obj) {
      if (obj.event === 'add') {
        window.add()
      } else if (obj.event === 'refresh') {
        window.refresh()
      } else if (obj.event === 'batchRemove') {
        window.batchRemove(obj)
      } else if (obj.event === 'collasped') {
        $('.user-left').toggleClass('user-collasped')
        $('.user-main').toggleClass('user-collasped')
        table.resize()
      }
    })

    form.on('submit(article-query)', function (data) {
      window.refresh(data.field)
      return false
    })



    window.add = function () {
      layer.open({
        type: 2,
        title: '新增',
        shade: 0.1,
        area: ['550px', '550px'],
        content: MODULE_PATH + 'addArticle'
      })
    }

    window.edit = function (obj) {
      layer.open({
        type: 2,
        title: '修改',
        shade: 0.1,
        area: ['550px', '500px'],
        content: MODULE_PATH + 'edit/' + obj.data['id']
      })
    }

    window.remove = function (obj) {
      layer.confirm('确定要删除该文章', { icon: 3, title: '提示' }, function (index) {
        layer.close(index)
        let loading = layer.load()
        $.ajax({
          url: MODULE_PATH + '/article/remove/' + obj.data['id'],
          dataType: 'json',
          type: 'delete',
          success: function (result) {
            layer.close(loading)
            if (result.success) {
              popup.success(result.msg, function () {
                obj.del()
              })
            } else {
              popup.failure(result.msg)
            }
          }
        })
      })
    }

    window.batchRemove = function (obj) {
      let data = table.checkStatus(obj.config.id).data
      if (data.length === 0) {
        layer.msg('未选中数据', {
          icon: 3,
          time: 1000
        })
        return false
      }
      var ids = []
      var hasCheck = table.checkStatus('article-table')
      var hasCheckData = hasCheck.data
      if (hasCheckData.length > 0) {
        $.each(hasCheckData, function (index, element) {
          ids.push(element.id)
        })
      }
      {#console.log(ids);#}
      layer.confirm('确定要删除选中角色', {
        icon: 3,
        title: '提示'
      }, function (index) {
        layer.close(index)
        let loading = layer.load()
        $.ajax({

          url: MODULE_PATH + 'batchRemove',
          data: { ids: ids },
          dataType: 'json',
          type: 'delete',
          success: function (result) {
            layer.close(loading)
            if (result.success) {
              popup.success(result.msg, function () {
                table.reload('article-table')
              })
            } else {
              popup.failure(result.msg)
            }
          }
        })
      })
    }

    window.refresh = function (param) {
      table.reload('article-table', { where: param })
    }
})
</script>
</html>